package com.huiquan.vocab.dao;

import com.huiquan.analysis.domain.User;
import com.huiquan.framework.base.EnhancedBaseDao;
import com.huiquan.framework.constant.BaseContants;
import com.huiquan.vocab.domain.VocabElement;
import com.huiquan.vocab.domain.VocabElementClashDto;
import com.huiquan.vocab.domain.VocabElementDto;
import com.ibatis.sqlmap.client.SqlMapClient;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class VocabElementDao extends EnhancedBaseDao<VocabElement> {

    @Resource(name = "sqlMapClient")
    private SqlMapClient sqlMapClient;

    public static final String space = "VOCAB_ELEMENT.";

    public VocabElementDao() {
        super(space);
    }

    public boolean retrieveCheckedByLastCheck(String propertyKey) {
        Map<String, Object> param = new HashMap<>();
        param.put("property", propertyKey);

        return (boolean) this.object(space + "RETRIEVE_CHECKED_BY_LAST_CHECK", param, sqlMapClient);
    }

    public void updateFlagByIds(String ids, Integer flag, User user) {
        Map<String, Object> param = new HashMap<>();
        param.put("ids", ids);
        param.put("flag", flag);
        param.put("userId", user.getUserId());
        param.put("userName", user.getRealName());

        this.update(space + "UPDATE_FLAG_BY_IDS", param, sqlMapClient);
    }

    public void updateReplaceVocab(Long id, String replaceVocab, User user) {
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("replaceVocab", replaceVocab);
        param.put("userId", user.getUserId());
        param.put("userName", user.getRealName());

        this.update(space + "UPDATE_REPLACE_VOCAB", param, sqlMapClient);
    }

    public void updateReplacePropAndFlag(Long id, String replaceProp, Integer flag, User user) {
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("replaceProp", replaceProp);
        param.put("userId", user.getUserId());
        param.put("userName", user.getRealName());
        param.put("flag", BaseContants.FLAG_WRONG);

        this.update(space + "UPDATE_REPLACE_PROP_AND_FLAG", param, sqlMapClient);
    }

    public VocabElement retrieveReplaceElement(Long id, String replaceProp) {
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("replaceProp", replaceProp);

        return (VocabElement) object(space + "RETRIEVE_REPLACE_ELEMENT", param, sqlMapClient);
    }

    public VocabElement retrieveObjectByVocabAndProp(String vocabulary, String property) {
        Map<String, Object> param = new HashMap<>();
        param.put("vocabulary", vocabulary);
        param.put("property", property);

        return (VocabElement) object(space + "RETRIEVE_OBJECT_BY_VOCABULARY_AND_PROP", param, sqlMapClient);
    }

    public void updateSlang(Long id, String slang, User user) {
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("slang", slang);
        param.put("userId", user.getUserId());
        param.put("userName", user.getRealName());

        this.update(space + "UPDATE_MAPPING_SLANG", param, sqlMapClient);
    }

    @SuppressWarnings("unchecked")
    public List<String> retrieveNotFilledReplaceVocab() {
        return (List<String>) list(space + "RETRIEVE_NOT_FILLED_REPLACE_VOCAB", sqlMapClient);
    }

    @SuppressWarnings("unchecked")
    public List<String> retrieveNotFilledSlang() {
        return (List<String>) list(space + "RETRIEVE_NOT_FILLED_SLANG", sqlMapClient);
    }

    public void updateReplaceProp(Long id, String replaceProp, User user) {
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("replaceProp", replaceProp);
        param.put("userId", user.getUserId());
        param.put("userName", user.getRealName());

        this.update(space + "UPDATE_REPLACE_PROP_AND_FLAG", param, sqlMapClient);
    }

    public VocabElement retrieveObjectByVocabularyAndReplaceProp(String vocabulary, String replaceProp) {
        Map<String, Object> param = new HashMap<>();
        param.put("vocabulary", vocabulary);
        param.put("replaceProp", replaceProp);

        return (VocabElement) object(space + "RETRIEVE_OBJECT_BY_VOCABULARY_AND_REPLACE_PROP", param, sqlMapClient);
    }

    public VocabElement retrieveObjectByReplaceVocabAndProp(String replaceVocab, String property) {
        Map<String, Object> param = new HashMap<>();
        param.put("replaceVocab", replaceVocab);
        param.put("property", property);

        return (VocabElement) object(space + "RETRIEVE_OBJECT_BY_REPLACE_VOCAB_AND_PROPERTY", param, sqlMapClient);
    }

    public void setSex(String id, String sex, User user) {
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("sex", sex);
        param.put("userId", user.getUserId());
        param.put("userName", user.getRealName());

        this.update(space + "SET_SEX", param, sqlMapClient);
    }

    public int setSynSex(String id, String sex, User user) {
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("sex", sex);
        param.put("userId", user.getUserId());
        param.put("userName", user.getRealName());

        return this.update(space + "SET_SYN_SEX", param, sqlMapClient);
    }

    public VocabElement getsynonymyelement(String id) {
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        return (VocabElement) object(space + "GET_SYN", param, sqlMapClient);
    }

    public void setAge(String id, String age, User user) {
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("age", age);
        param.put("userId", user.getUserId());
        param.put("userName", user.getRealName());
        this.update(space + "SET_AGE", param, sqlMapClient);
    }

    public int setSynAge(String id, String age, User user) {
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("age", age);
        param.put("userId", user.getUserId());
        param.put("userName", user.getRealName());
        return this.update(space + "SET_SYN_AGE", param, sqlMapClient);
    }

    public void submitSexAge(String ids, User user) {
        Map<String, Object> param = new HashMap<>();
        param.put("ids", "(" + ids + ")");
        param.put("userId", user.getUserId());
        param.put("userName", user.getRealName());

        this.update(space + "SUBMIT_SEX_AGE", param, sqlMapClient);
    }

    @SuppressWarnings("unchecked")
    public List<VocabElement> retrievePreElementList(String vocab, String prop, long id) {
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("vocab", vocab);
        param.put("prop", prop);

        return (List<VocabElement>) this.list(space + "RETRIEVE_PRE_ELEMENT_LIST", param, sqlMapClient);
    }

    @SuppressWarnings("unchecked")
    public List<VocabElement> retrieveReplaceListByIds(String ids) {
        Map<String, Object> param = new HashMap<>();
        param.put("ids", ids);

        return (List<VocabElement>) this.list(space + "RETRIEVE_REPLACE_LIST_BY_IDS", param, sqlMapClient);
    }

    @SuppressWarnings("unchecked")
    public List<VocabElement> retrievePreListByIds(String ids) {
        Map<String, Object> param = new HashMap<>();
        param.put("ids", ids);

        return (List<VocabElement>) this.list(space + "RETRIEVE_PRE_LIST_BY_IDS", param, sqlMapClient);
    }

    public VocabElement retrieveTheLargestCntWordByWords(List<String> words, String property) {
        Map<String, Object> param = new HashMap<>();
        param.put("property", property);
        param.put("words", words);
        param.put("ordreStr", "cnt desc");

        return (VocabElement) this.object(space + "RETRIEVE_OBJECT", param, sqlMapClient);
    }

    @SuppressWarnings("unchecked")
    public List<VocabElementDto> retrieveDtoList(Map<String, Object> param) {
        return (List<VocabElementDto>) this.list(space + "RETRIEVE_DTO_LIST", param, sqlMapClient);
    }

    /**
     * 替换后词语没有拆分且为错误的词
     *
     * @return
     */
    @SuppressWarnings("unchecked")
    public List<VocabElementClashDto> retrieveReplaceResultWrong() {

        return (List<VocabElementClashDto>) this.list(space + "RETRIEVE_REPLACE_RESULT_WRONG", sqlMapClient);
    }

    /**
     * 拆分后为错误的词
     *
     * @return
     */
    @SuppressWarnings("unchecked")
    public List<VocabElementClashDto> retrieveSplitResultWrong() {

        return (List<VocabElementClashDto>) this.list(space + "RETRIEVE_SPLIT_RESULT_WRONG", sqlMapClient);
    }

    /**
     * 将正确的小词都设置为同义词已校验
     */
    public void checkSynonymyFlag(String property) {

        this.update(space + "CHECK_SYNONYMY_FLAG", property, sqlMapClient);
    }

    /**
     * 获取由s替换成si的小词名称
     */
    @SuppressWarnings("unchecked")
    public List<String> retrieveSReplaceSi() {
        return (List<String>) this.list(space + "RETRIEVE_S_REPLACE_SI", sqlMapClient);
    }

    /**
     * 判断疾病名称是否是正确的di
     */
    public int retrieveIsDi(String name) {
        Map<String, Object> param = new HashMap<>();
        param.put("name", name);
        Integer size = (Integer) this.object(space + "RETRIEVE_DI_SIZE", param, sqlMapClient);
        return size == null ? 0 : size;
    }

    /**
     * 查询拆分规则条数
     */
    public int retrieveDtoSize(Map<String, Object> param) {
        Integer size = (Integer) this.object(space + "RETRIEVE_DTO_SIZE", param, sqlMapClient);
        return size == null ? 0 : size;
    }

    public void changeCheckFlag(List<String> id, String checkFlag) {
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("checkFlag", checkFlag);
        this.update(space + "CHANGE_CHECK_FLAG", param, sqlMapClient);
    }

    public void updateQuantization(long id, String quantization, User user) {
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("quantization", quantization);
        param.put("userId", user.getUserId());
        param.put("userName", user.getRealName());

        this.update(space + "UPDATE_QUANTIZATION", param, sqlMapClient);
    }

    /**
     * 获取该同义词的量化
     */
    @SuppressWarnings("unchecked")
    public List<VocabElement> retrieveSynonymyQuantization(String synonymyId) {
        Map<String, Object> param = new HashMap<>();
        param.put("synonymyId", synonymyId);
        return (List<VocabElement>) this.list(space + "RETRIEVE_SYNONYMY_QUANTIZATION", param, sqlMapClient);
    }

    /**
     * 初始化t同义词
     */
    public void initTSynonymy() {
        Map<String, Object> param = new HashMap<>();
        param.put("regex", "^[u0391-uFFE5/!@#$%^&*()_+-=,./;\\<>?:{}|`~]*$");
        this.update(space + "INIT_T_SYNONYMY", param, sqlMapClient);
    }

    @SuppressWarnings("unchecked")
    public List<Map<String, String>> retrieveElementAndStdExceptO() {

        return (List<Map<String, String>>) this.list(space + "RETRIEVE_ELEMENT_AND_STD_EXCEPT_O", sqlMapClient);
    }

    @SuppressWarnings("unchecked")
    public List<Map<String, String>> retrieveElementAndStdO() {

        return (List<Map<String, String>>) this.list(space + "RETRIEVE_ELEMENT_AND_STD_O", sqlMapClient);
    }

    @SuppressWarnings("unchecked")
    public List<Map<String, String>> retrieveElementReplace() {

        return (List<Map<String, String>>) this.list(space + "RETRIEVE_ELEMENT_REPLACE", sqlMapClient);
    }

    public void checkChangeResult() {
        this.update(space + "CHECK_CHANGE_RESULT", sqlMapClient);
    }

    @SuppressWarnings("unchecked")
    public List<VocabElement> retrieveByIds(String ids) {
        Map<String, Object> param = new HashMap<>();
        param.put("ids", ids);
        param.put("all", "1");
        return (List<VocabElement>) this.list(space + "RETRIEVE_OBJECT", param, sqlMapClient);
    }

    public void updateAnalysisFlag(long id, String analysisFlag) {
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("analysisFlag", analysisFlag);

        this.update(space + "UPDATE_ANALYSIS_FLAG", param, sqlMapClient);
    }

    @SuppressWarnings("unchecked")
    public List<Map<String, String>> retrieveElementSplit() {

        return (List<Map<String, String>>) this.list(space + "RETRIEVE_ELEMENT_SPLIT", sqlMapClient);
    }

    @SuppressWarnings("unchecked")
    public List<VocabElementDto> retrieveMainList(Map<String, Object> param) {
        return (List<VocabElementDto>) this.list(space + "RETRIEVE_MAIN_LIST", param, sqlMapClient);
    }

    @SuppressWarnings("unchecked")
    public List<Map<String, Object>> retrieveDetails(String ids) {
        return (List<Map<String, Object>>) this.list(space + "RETRIEVE_DETAILS", ids, sqlMapClient);
    }

    @SuppressWarnings("unchecked")
    public List<String> retrieveSynonymUnchecked(String propertyKey) {
        return (List<String>) this.list(space + "RETRIEVE_SYNONYM_UNCHECKED", propertyKey, sqlMapClient);
    }

    public void initSexAgeSynonymy() {
        this.update(space + "INIT_SEX_AGE_SYNONYMY", sqlMapClient);
    }

    public Long insertChangeRule(Long id) {
        Map<String, Object> param = new HashMap<>();
        param.put("eid", id);
		return (Long)this.insert(space + "INSERT_CHANGE_RULE", param,sqlMapClient);
    }

    public Long insertSource(Long id) {
        Map<String, Object> param = new HashMap<>();
        param.put("eid", id);

        return (Long)this.insert(space + "INSERT_SOURCE", param,sqlMapClient);
    }
}
